package com.ruoyi.quartz.task;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.HttpUrlConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.quartz.domain.StockDailySina;
import com.ruoyi.quartz.domain.StockStageIncomeThree;
import com.ruoyi.quartz.mapper.StockBasicMapper;
import com.ruoyi.quartz.mapper.StockDailySinaMapper;
import com.ruoyi.quartz.mapper.StockStageIncomeThreeMapper;
import com.xuxueli.crawler.XxlCrawler;
import com.xuxueli.crawler.parser.strategy.NonPageParser;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component("sinaCrawlerTask")
public class SinaCrawlerTask {

    @Resource
    StockDailySinaMapper stockDailySinaMapper;

    @Resource
    StockStageIncomeThreeMapper stockStageIncomeThreeMapper;

    @Resource
    StockBasicMapper stockBasicMapper;

    /**
     * 爬取新浪财经日交易数据
     * @param pageNums
     * @param pageSize
     */
    public void stockDaily (Integer pageNums,Integer pageSize){

        if (!stockBasicMapper.getTradeDateCount(DateUtils.dateTime())){
            return;
        }

        String[] urls = new String[pageNums];
        String tmpUrl = "";
        int j = 0;

        for (int i =0;i<urls.length;i++){
            j = i+1;
            tmpUrl = HttpUrlConstants.SINA_STOCK_DAILY_URL.replaceAll("pageNums",""+j);
            tmpUrl = tmpUrl.replaceAll("pageSize",""+pageSize);
            urls[i] = tmpUrl;
        }

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(urls)
                .setPauseMillis(5000)
                .setPageParser(new NonPageParser() {
                    @Override
                    public void parse(String url, String pageSource) {
                        JSONArray result = JSON.parseArray(pageSource);
                        List<StockDailySina> list = new ArrayList<>();
                        StockDailySina sds = null;
                        JSONObject tmp = null;
                        for (int i=0;i<result.size();i++){
                            tmp = result.getJSONObject(i);
                            sds = new StockDailySina();
                            sds.setSymbol(tmp.getString("symbol"));
                            sds.setCode(tmp.getString("code"));
                            sds.setName(tmp.getString("name"));
                            sds.setTrade(tmp.getBigDecimal("trade"));
                            sds.setPriceChange(tmp.getBigDecimal("pricechange"));
                            sds.setChangePercent(tmp.getBigDecimal("changepercent"));
                            sds.setSettlement(tmp.getBigDecimal("settlement"));
                            sds.setOpenPrice(tmp.getBigDecimal("open"));
                            sds.setHigh(tmp.getBigDecimal("high"));
                            sds.setLow(tmp.getBigDecimal("low"));
                            sds.setVolume(tmp.getLong("volume"));
                            sds.setAmount(tmp.getLong("amount"));
                            sds.setPer(tmp.getBigDecimal("per"));
                            sds.setPb(tmp.getBigDecimal("pb"));
                            sds.setMktcap(tmp.getBigDecimal("mktcap"));
                            sds.setNmc(tmp.getBigDecimal("nmc"));
                            sds.setTurnoverRatio(tmp.getBigDecimal("turnoverratio"));
                            sds.setTradeDate(DateUtils.dateTime());
                            list.add(sds);
                        }
                        stockDailySinaMapper.batchInsertStockDailySina(list);

                    }
                })
                .build();
        crawler.start(true);
    }

    /**
     * 爬取当日主力净流入*
     * @param pageNums
     */
    public void stockIncomeNow (Integer pageNums){

        if (!stockBasicMapper.getTradeDateCount(DateUtils.dateTime())){
            return;
        }

        String[] urls = new String[pageNums];
        String tmpUrl = "";
        int j = 0;

        for (int i =0;i<urls.length;i++){
            j = i+1;
            tmpUrl = HttpUrlConstants.SINA_STOCK_MAIN_INCOME_NOW_URL.replaceAll("pageNums",""+j);
            urls[i] = tmpUrl;
        }

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(urls)
                .setPauseMillis(5000)
                .setPageParser(new NonPageParser() {
                    @Override
                    public void parse(String url, String pageSource) {
                        JSONArray result = JSON.parseArray(pageSource);
                        StockStageIncomeThree stockStageIncomeThree = null;
                        List<StockStageIncomeThree> list = new ArrayList<>();
                        JSONObject tmp = null;
                        for (int i=0;i<result.size();i++){
                            tmp = result.getJSONObject(i);
                            stockStageIncomeThree = new StockStageIncomeThree();
                            stockStageIncomeThree.setSymbol(tmp.getString("symbol"));
                            stockStageIncomeThree.setStagePrice(tmp.getBigDecimal("trade"));
                            stockStageIncomeThree.setStageChange(tmp.getBigDecimal("changeratio"));
                            stockStageIncomeThree.setStageTurnover(tmp.getBigDecimal("turnover"));
                            stockStageIncomeThree.setStageIncome(tmp.getBigDecimal("netamount"));
                            stockStageIncomeThree.setStageIncomeRate(tmp.getBigDecimal("ratioamount"));
                            stockStageIncomeThree.setMainIncome(tmp.getBigDecimal("r0_net"));
                            stockStageIncomeThree.setMainIncomeRate(tmp.getBigDecimal("r0_ratio"));
                            stockStageIncomeThree.setMainCompass(tmp.getBigDecimal("r0x_ratio"));
                            stockStageIncomeThree.setCollectDate(DateUtils.dateTime());
                            list.add(stockStageIncomeThree);

                        }
                        stockStageIncomeThreeMapper.batchInsertStockIncome(list);

                    }
                })
                .build();
        crawler.start(true);

    }

    /**
     * 爬取新浪近三日主力净流入情况*
     * @param pageNums
     */
    public void stockIncomeRecentThree (Integer pageNums){

        if (!stockBasicMapper.getTradeDateCount(DateUtils.dateTime())){
            return;
        }

        String[] urls = new String[pageNums];
        String tmpUrl = "";
        int j = 0;


        for (int i =0;i<urls.length;i++){
            j = i+1;
            tmpUrl = HttpUrlConstants.SINA_STOCK_MAIN_INCOME_RECENT_URL.replaceAll("pageNums",""+j);
            tmpUrl = tmpUrl.replaceAll("recentDay","3");
            urls[i] = tmpUrl;
        }

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(urls)
                .setPauseMillis(5000)
                .setPageParser(new NonPageParser() {
                    @Override
                    public void parse(String url, String pageSource) {
                        JSONArray result = JSON.parseArray(pageSource);
                        StockStageIncomeThree stockStageIncomeThree = null;
                        List<StockStageIncomeThree> list = new ArrayList<>();
                        JSONObject tmp = null;
                        for (int i=0;i<result.size();i++){
                            tmp = result.getJSONObject(i);
                            stockStageIncomeThree = new StockStageIncomeThree();
                            stockStageIncomeThree.setSymbol(tmp.getString("symbol"));
                            stockStageIncomeThree.setStagePrice(tmp.getBigDecimal("trade"));
                            stockStageIncomeThree.setStageChange(tmp.getBigDecimal("changeratio"));
                            stockStageIncomeThree.setStageTurnover(tmp.getBigDecimal("turnover"));
                            stockStageIncomeThree.setStageIncome(tmp.getBigDecimal("netamount"));
                            stockStageIncomeThree.setStageIncomeRate(tmp.getBigDecimal("ratioamount"));
                            stockStageIncomeThree.setMainIncome(tmp.getBigDecimal("r0_net"));
                            stockStageIncomeThree.setMainIncomeRate(tmp.getBigDecimal("r0_ratio"));
                            stockStageIncomeThree.setMainCompass(tmp.getBigDecimal("r0x_ratio"));
                            stockStageIncomeThree.setCollectDate(DateUtils.dateTime());
                            list.add(stockStageIncomeThree);

                        }
                        stockStageIncomeThreeMapper.batchInsertStockIncomeThree(list);

                    }
                })
                .build();
        crawler.start(true);

    }

    /**
     * 爬取新浪近五日主力净流入情况*
     * @param pageNums
     */
    public void stockIncomeRecentFive (Integer pageNums){

        if (!stockBasicMapper.getTradeDateCount(DateUtils.dateTime())){
            return;
        }

        String[] urls = new String[pageNums];
        String tmpUrl = "";
        int j = 0;

        for (int i =0;i<urls.length;i++){
            j = i+1;
            tmpUrl = HttpUrlConstants.SINA_STOCK_MAIN_INCOME_RECENT_URL.replaceAll("pageNums",""+j);
            tmpUrl = tmpUrl.replaceAll("recentDay","5");
            urls[i] = tmpUrl;
        }

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(urls)
                .setPauseMillis(5000)
                .setPageParser(new NonPageParser() {
                    @Override
                    public void parse(String url, String pageSource) {
                        JSONArray result = JSON.parseArray(pageSource);
                        StockStageIncomeThree stockStageIncomeThree = null;
                        List<StockStageIncomeThree> list = new ArrayList<>();
                        JSONObject tmp = null;
                        for (int i=0;i<result.size();i++){
                            tmp = result.getJSONObject(i);
                            stockStageIncomeThree = new StockStageIncomeThree();
                            stockStageIncomeThree.setSymbol(tmp.getString("symbol"));
                            stockStageIncomeThree.setStagePrice(tmp.getBigDecimal("trade"));
                            stockStageIncomeThree.setStageChange(tmp.getBigDecimal("changeratio"));
                            stockStageIncomeThree.setStageTurnover(tmp.getBigDecimal("turnover"));
                            stockStageIncomeThree.setStageIncome(tmp.getBigDecimal("netamount"));
                            stockStageIncomeThree.setStageIncomeRate(tmp.getBigDecimal("ratioamount"));
                            stockStageIncomeThree.setMainIncome(tmp.getBigDecimal("r0_net"));
                            stockStageIncomeThree.setMainIncomeRate(tmp.getBigDecimal("r0_ratio"));
                            stockStageIncomeThree.setMainCompass(tmp.getBigDecimal("r0x_ratio"));
                            stockStageIncomeThree.setCollectDate(DateUtils.dateTime());
                            list.add(stockStageIncomeThree);

                        }
                        stockStageIncomeThreeMapper.batchInsertStockIncomeFive(list);

                    }
                })
                .build();
        crawler.start(true);

    }

    /**
     * 爬取新浪近十日主力净流入情况*
     * @param pageNums
     */
    public void stockIncomeRecentTen (Integer pageNums){

        if (!stockBasicMapper.getTradeDateCount(DateUtils.dateTime())){
            return;
        }

        String[] urls = new String[pageNums];
        String tmpUrl = "";
        int j = 0;

        for (int i =0;i<urls.length;i++){
            j = i+1;
            tmpUrl = HttpUrlConstants.SINA_STOCK_MAIN_INCOME_RECENT_URL.replaceAll("pageNums",""+j);
            tmpUrl = tmpUrl.replaceAll("recentDay","10");
            urls[i] = tmpUrl;
        }

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(urls)
                .setPauseMillis(5000)
                .setPageParser(new NonPageParser() {
                    @Override
                    public void parse(String url, String pageSource) {
                        JSONArray result = JSON.parseArray(pageSource);
                        StockStageIncomeThree stockStageIncomeThree = null;
                        List<StockStageIncomeThree> list = new ArrayList<>();
                        JSONObject tmp = null;
                        for (int i=0;i<result.size();i++){
                            tmp = result.getJSONObject(i);
                            stockStageIncomeThree = new StockStageIncomeThree();
                            stockStageIncomeThree.setSymbol(tmp.getString("symbol"));
                            stockStageIncomeThree.setStagePrice(tmp.getBigDecimal("trade"));
                            stockStageIncomeThree.setStageChange(tmp.getBigDecimal("changeratio"));
                            stockStageIncomeThree.setStageTurnover(tmp.getBigDecimal("turnover"));
                            stockStageIncomeThree.setStageIncome(tmp.getBigDecimal("netamount"));
                            stockStageIncomeThree.setStageIncomeRate(tmp.getBigDecimal("ratioamount"));
                            stockStageIncomeThree.setMainIncome(tmp.getBigDecimal("r0_net"));
                            stockStageIncomeThree.setMainIncomeRate(tmp.getBigDecimal("r0_ratio"));
                            stockStageIncomeThree.setMainCompass(tmp.getBigDecimal("r0x_ratio"));
                            stockStageIncomeThree.setCollectDate(DateUtils.dateTime());
                            list.add(stockStageIncomeThree);

                        }
                        stockStageIncomeThreeMapper.batchInsertStockIncomeTen(list);

                    }
                })
                .build();
        crawler.start(true);

    }

    /**
     * 爬取新浪近二十日主力净流入情况*
     * @param pageNums
     */
    public void stockIncomeRecentTwenty (Integer pageNums){

        if (!stockBasicMapper.getTradeDateCount(DateUtils.dateTime())){
            return;
        }

        String[] urls = new String[pageNums];
        String tmpUrl = "";
        int j = 0;

        for (int i =0;i<urls.length;i++){
            j = i+1;
            tmpUrl = HttpUrlConstants.SINA_STOCK_MAIN_INCOME_RECENT_URL.replaceAll("pageNums",""+j);
            tmpUrl = tmpUrl.replaceAll("recentDay","20");
            urls[i] = tmpUrl;
        }

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(urls)
                .setPauseMillis(5000)
                .setPageParser(new NonPageParser() {
                    @Override
                    public void parse(String url, String pageSource) {
                        JSONArray result = JSON.parseArray(pageSource);
                        StockStageIncomeThree stockStageIncomeThree = null;
                        List<StockStageIncomeThree> list = new ArrayList<>();
                        JSONObject tmp = null;
                        for (int i=0;i<result.size();i++){
                            tmp = result.getJSONObject(i);
                            stockStageIncomeThree = new StockStageIncomeThree();
                            stockStageIncomeThree.setSymbol(tmp.getString("symbol"));
                            stockStageIncomeThree.setStagePrice(tmp.getBigDecimal("trade"));
                            stockStageIncomeThree.setStageChange(tmp.getBigDecimal("changeratio"));
                            stockStageIncomeThree.setStageTurnover(tmp.getBigDecimal("turnover"));
                            stockStageIncomeThree.setStageIncome(tmp.getBigDecimal("netamount"));
                            stockStageIncomeThree.setStageIncomeRate(tmp.getBigDecimal("ratioamount"));
                            stockStageIncomeThree.setMainIncome(tmp.getBigDecimal("r0_net"));
                            stockStageIncomeThree.setMainIncomeRate(tmp.getBigDecimal("r0_ratio"));
                            stockStageIncomeThree.setMainCompass(tmp.getBigDecimal("r0x_ratio"));
                            stockStageIncomeThree.setCollectDate(DateUtils.dateTime());
                            list.add(stockStageIncomeThree);

                        }
                        stockStageIncomeThreeMapper.batchInsertStockIncomeTwenty(list);

                    }
                })
                .build();
        crawler.start(true);

    }

    /**
     * 爬取新浪近六十日主力净流入情况*
     * @param pageNums
     */
    public void stockIncomeRecentSixty (Integer pageNums){

        if (!stockBasicMapper.getTradeDateCount(DateUtils.dateTime())){
            return;
        }

        String[] urls = new String[pageNums];
        String tmpUrl = "";
        int j = 0;

        for (int i =0;i<urls.length;i++){
            j = i+1;
            tmpUrl = HttpUrlConstants.SINA_STOCK_MAIN_INCOME_RECENT_URL.replaceAll("pageNums",""+j);
            tmpUrl = tmpUrl.replaceAll("recentDay","60");
            urls[i] = tmpUrl;
        }

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(urls)
                .setPauseMillis(5000)
                .setPageParser(new NonPageParser() {
                    @Override
                    public void parse(String url, String pageSource) {
                        JSONArray result = JSON.parseArray(pageSource);
                        List<StockDailySina> list = new ArrayList<>();
                        StockDailySina sds = null;
                        JSONObject tmp = null;
                        for (int i=0;i<result.size();i++){
                            tmp = result.getJSONObject(i);

                        }

                    }
                })
                .build();
        crawler.start(true);

    }


}
